home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 212 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.7 KB

  1. From: marek@iiasa.ac.at (Marek  MAKOWSKI)
  2. Message-ID: <1996Feb1.091641.4676@iiasa.ac.at>
  3. X-Original-Date: Thu, 1 Feb 1996 09:16:41 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 01 Feb 96 15:49:11 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: new T[0] and sizeof(T)
  9. Organization: IIASA, Laxenburg, Austria
  10. X-Newsreader: TIN [version 1.2 PL2]
  11. Content-Type: text
  12. Apparently-To: comp-std-c++@uunet.uu.net
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBFAgUBMRDhCOEDnX0m9pzZAQEoLgF9Fu912ML2kPk5zZDDDnuHrPJEPdnv9Aks
  15.     LLJcx2xXJeCS94hjc9Zgnv5yHcZ6Whzh
  16.     =unOH
  17.  
  18. I would like to ask for comments on three easy questions illustrated 
  19. by the following piece of code:
  20.  
  21. template <class I, class T>
  22. void mVect<I,T>::resize(I new_size) {
  23.    T *old = v;
  24.    v = new T[new_size];    // T *v is a private member of mVect  
  25.    int size_of_elem = sizeof(T);    // <--- question 3 
  26.    // 
  27.    // do something 
  28.    // 
  29.    delete[] old;     // <--- questions 1 & 2 
  30.  
  31. I have the following questions:
  32. 1. Is it absolutely robust and portable to delete[] old, even
  33.    if a previous call was for new_size == 0 (or if v was allocated
  34.    by the ctor for the size == 0) ?
  35.     In other words: is it guaranteed that:
  36.    (v = new T[0]) == 0;
  37. 2. Is it correct to assume that no destructor is called by this statement
  38.     if old was set as: old = new T[0]  ??
  39. 3. Is there any risk involved in using sizeof(T) in this statement ?
  40.  
  41. I used to be sure that the answers are:
  42. 1. Yes
  43. 2. Yes
  44. 3. No
  45.  
  46. However, I have recently started to port an application developed
  47. with SunPro (previously ported to Borland and checked with the
  48. Bounds-Checker32).
  49. The Watcom code blows-up on the delete[] statement (if a previous
  50. call was for new_size == 0) and gives a warning whenever it sees the 
  51. sizeof(T).
  52.  
  53. If the answer for quaestions 1 and 2 is negative then I would like to
  54. know the reason why the standard does not require new to return 0
  55. for zero_size array of objects.
  56. This would work nicely with the delete guaranteed to be safe for
  57. the 0 argument.
  58. If there is a good reason for allowing new to return "anything" in such
  59. situations then one should add to every ctor setting a ptr to 0
  60. (which indeed made the Watcom version of my application running).
  61.  
  62. Thanks in advance for your time.
  63.  
  64. Marek
  65. -- 
  66.                   Marek Makowski  | Email: marek@iiasa.ac.at
  67.          International Institute  | Phone: (+43-2236) 807.561
  68.     for Applied Systems Analysis  | Fax:   (+43-2236) 71.313
  69.        A-2361 Laxenburg, Austria  | Web: http://www.iiasa.ac.at
  70. ---
  71. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  72.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  73.   is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
  74.